package com.lujieni.springbootlock4j.controller;

import com.baomidou.lock.annotation.Lock4j;
import com.lujieni.springbootlock4j.vo.StudentVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author：小鹿爱吃鱼
 * @Date: 2025/5/29 8:47
 * @Description:
 */
@RestController
public class LockController {

    /**
     * key: springboot-lock4j:add#3  yml中的前缀:name
     * keys = {"#id"}会带上入参的id    这里#是规定要写的
     * keys = {"#vo.getUserId"}      从对象中获取字段当做key
     * @param studentVO
     *
     * @Lock4j(name = "add", keys = {"#studentVO.getId"}, acquireTimeout = 0)
     *
     * springboot-lock4j:add#19911017
     */
    @GetMapping("/v1/add")
    @Lock4j(name = "add", keys = {"#studentVO.getId"}, acquireTimeout = 3000)
    public void addStudent(StudentVO studentVO){
        System.out.println("addStudent");
        System.out.println("add");
        test(100);
    }

    /**
     * 单纯以方法名当做key,并发度很低
     * key: springboot-lock4j:com.lujieni.springbootlock4j.controller.LockControllerminus
     */
    @GetMapping("/v1/minus")
    @Lock4j
    public void minus(){
        System.out.println("add");
        test(10);
    }

    /**
     * 测试无锁
     *
     * @param processTime 业务处理时间
     * @return {@link Long}
     */
    public Long test(long processTime) {
        try {
            Thread.sleep(processTime * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return processTime;
    }

}
